//
//  ESTActivityTableViewCell.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/9/29.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTActivityTableViewCell.h"
#import "ESTActivityCellHeaderView.h"
#import "ESTVerticalValueView.h"
#import "ESTActivityCellView.h"
#import "ESTActivityHomeModel.h"
#import "ESTActivityModel.h"
@interface ESTActivityTableViewCell ()

@end

@implementation ESTActivityTableViewCell

/**
 创建我的业绩Cell

 @param activityData 所有活动数据
 @param activityInteger 选择了第几个活动
 @param dateType 日周月类型
 @param dataModel 所有数据
 @return 创建我的业绩Cell
 */
+ (instancetype)activityAchieveCellWithActivityData:(NSArray<ESTActivityModel *> *)activityData activityInteger:(NSInteger)activityInteger dateType:(NSInteger)dateType dataModel:(ESTActivityHomeModel *)dataModel {
    ESTActivityTableViewCell *cell = [[ESTActivityTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ESTActivityTableViewCell"];
    [cell.contentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    __weak typeof(cell) weakCell = cell;
    TYZImageView *backgroundView = [[TYZImageView alloc] initWithImage:[UIImage imageNamed:@"home_top_bg"]];
    [cell.contentView addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(cell.contentView);
    }];
    backgroundView.userInteractionEnabled = YES;
    TYZLabel *titleLabel = [[TYZLabel alloc] init];
    [cell.contentView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(cell.contentView);
        make.top.equalTo(cell.contentView).offset(10);
        make.height.mas_equalTo(20);
    }];
    titleLabel.font = TYZFont(CoreInfo.fontSize * 1.2);
    titleLabel.textColor = TYZWhiteColor;
    titleLabel.text = [NSString stringWithFormat:@"%@，%@！", UserInfo.EMP_INFO.firstObject.EMPLOYEE_NAME, BaseInfo.regard];
    ESTActivityCellHeaderView *headerView = [[ESTActivityCellHeaderView alloc] initWithActivityData:(NSArray<ESTActivityModel *> *)activityData activityInteger:(NSInteger)activityInteger dateType:dateType];
    [cell.contentView addSubview:headerView];
    cell.contentView.backgroundColor = BaseInfo.themeColor;
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(-5);
        make.left.right.equalTo(cell.contentView);
        make.height.mas_equalTo(ActivityCell_Height);
    }];
    [headerView setDidClickChooseActivityBlock:^(NSArray<NSNumber *> *numberArray) {
        if (weakCell.didClickChooseActivityBlock) {
            weakCell.didClickChooseActivityBlock(numberArray);
        }
    }];
    [headerView setDidClickDayWeekMonthBlock:^(NSInteger integer) {
        if (weakCell.didClickDayWeekMonthBlock) {
            weakCell.didClickDayWeekMonthBlock(integer);
        }
    }];
    
    CGFloat width = kWindowWidth / 4;
    CGFloat height = (300 - 30 - 50) / 3;
    NSArray<NSString *> *valueArr = dataModel.achieveValueArray;
    for (NSInteger i = 0; i < 3; i++) {
        for (NSInteger j = 0; j < 4; j++) {
            if (i == 2 && j == 3) {
                break;
            }
            ESTVerticalValueView *childView = [ESTVerticalValueView valueViewWithName:BaseInfo.achieveTextArray[i * 4 + j] value:valueArr[i * 4 + j] backgroundColorModel:BackgroundColor_White];
            [cell.contentView addSubview:childView];
            [childView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(cell.contentView).offset(width * j);
                make.top.equalTo(cell.contentView).offset(50 + ActivityCell_Height + height * i);
                make.width.mas_equalTo(width);
                make.height.mas_equalTo(height);
            }];
            [childView setDidClickCoverBlock:^{
                if (weakCell.didClickMyAchieveBlock) {
                    weakCell.didClickMyAchieveBlock(i * 4 + j);
                }
            }];
        }
    }
    return cell;
}

/**
 创建待办事项的Cell

 @param dataModel 所有数据
 @return 创建待办事项的Cell
 */
+ (instancetype)activityMatterCellWithDataModel:(ESTActivityHomeModel *)dataModel {
    ESTActivityTableViewCell *cell = [[ESTActivityTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ESTActivityTableViewCell"];
    [cell.contentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    __weak typeof(cell) weakCell = cell;
    ESTActivityCellHeaderView *headerView = [[ESTActivityCellHeaderView alloc] initWithTitle:[@"待办事项" S]];
    [cell.contentView addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.contentView);
        make.left.right.equalTo(cell.contentView);
        make.height.mas_equalTo(ActivityCell_Height);
    }];
    cell.didClickDayWeekMonthBlock = headerView.didClickDayWeekMonthBlock;
    
    CGFloat width = kWindowWidth / 3;
    CGFloat height = 70;
    NSArray<NSString *> *valueArr = dataModel.matterValueArray;
    for (NSInteger i = 0; i < 3; i++) {
        ESTVerticalValueView *childView = [ESTVerticalValueView valueViewWithName:BaseInfo.matterTextArray[i] value:valueArr[i] backgroundColorModel:BackgroundColor_Black];
        [cell.contentView addSubview:childView];
        [childView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.contentView).offset(width * i);
            make.top.equalTo(cell.contentView).offset(ActivityCell_Height);
            make.width.mas_equalTo(width);
            make.height.mas_equalTo(height);
        }];
        [childView setDidClickCoverBlock:^{
            if (weakCell.didClickMatterBlock) {
                weakCell.didClickMatterBlock(i);
            }
        }];
    }
    return cell;
}

/**
 创建最新战报的Cell

 @param dataModel 所有数据
 @return 创建最新战报的Cell
 */
+ (instancetype)activityReportCellWithDataModel:(ESTActivityHomeModel *)dataModel {
    ESTActivityTableViewCell *cell = [[ESTActivityTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ESTActivityTableViewCell"];
    [cell.contentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    __weak typeof(cell) weakCell = cell;
    ESTActivityCellHeaderView *headerView = [[ESTActivityCellHeaderView alloc] initWithTitle:[@"最新战报" S] more:[@"排行榜" S]];
    [cell.contentView addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.contentView);
        make.left.right.equalTo(cell.contentView);
        make.height.mas_equalTo(ActivityCell_Height);
    }];
    [headerView setDidClickMoreBlock:^{
        if (weakCell.didClickChartsBlock) {
            weakCell.didClickChartsBlock();
        }
    }];
    TYZView *line = [[TYZView alloc] init];
    [cell.contentView addSubview:line];
    line.backgroundColor = TYZLightMMColor;
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(20);
        make.right.equalTo(cell.contentView).offset(-20);
        make.height.mas_equalTo(1);
        make.top.equalTo(headerView.mas_bottom);
    }];
    
    CGFloat width = kWindowWidth / 4;
    CGFloat height = (200 - ActivityCell_Height) / 4;
    NSArray<NSArray<NSString *> *> *valueArr = dataModel.reportValueArray;
    for (NSInteger i = 0; i < 4; i++) {
        for (NSInteger j = 0; j < 4; j++) {
            TYZLabel *label = [[TYZLabel alloc] init];
            [cell.contentView addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(line.mas_bottom).offset(height * i);
                make.left.equalTo(cell.contentView).offset(width * j);
                make.height.mas_equalTo(height);
                make.width.mas_equalTo(width);
            }];
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = TYZLightMColor;
            label.font = [UIFont systemFontOfSize:Activity_Home_Text_FontSize + 2];
            if (i == 0) {
                label.text = BaseInfo.reportHeaderArray[j];
                continue;
            }
            if (j == 0) {
                label.text = BaseInfo.reportStateArray[i - 1];
            } else {
                if (valueArr) {
                    label.text = valueArr[j - 1][i - 1];
                } else {
                    label.text = @"-";
                }
            }
        }
    }
    return cell;
}

/**
 创建最新活动的Cell

 @param activityModel 最新活动的模型
 @return 创建最新活动的Cell
 */
+ (instancetype)activityContentCellWithActivityModel:(ESTActivityModel *)activityModel {
    ESTActivityTableViewCell *cell = [[ESTActivityTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ESTActivityTableViewCell"];
    [cell.contentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    __weak typeof(cell) weakCell = cell;
    ESTActivityCellHeaderView *headerView = [[ESTActivityCellHeaderView alloc] initWithTitle:[@"最新活动" S] more:[@"更多" S]];
    [cell.contentView addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.contentView);
        make.left.right.equalTo(cell.contentView);
        make.height.mas_equalTo(ActivityCell_Height);
    }];
    [headerView setDidClickMoreBlock:^{
        if (weakCell.didClickMoreActivityBlock) {
            weakCell.didClickMoreActivityBlock();
        }
    }];
    TYZView *line = [[TYZView alloc] init];
    [cell.contentView addSubview:line];
    line.backgroundColor = TYZLightMMColor;
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(20);
        make.right.equalTo(cell.contentView).offset(-20);
        make.height.mas_equalTo(1);
        make.top.equalTo(headerView.mas_bottom);
    }];
    
    ESTActivityCellView *cellView = [ESTActivityCellView creatActivityCellView];
    [cellView setDidClickCoverBlock:^{
        if (weakCell.didClickActivityDetailBlock) {
            weakCell.didClickActivityDetailBlock(0);
        }
    }];
    [cell.contentView addSubview:cellView];
    [cellView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(cell.contentView);
        make.top.equalTo(line.mas_bottom).offset(5);
    }];
    cellView.iconString = activityModel.ACTIVITY_ATTA_URL1;
    cellView.title = activityModel.ACTIVITY_NAME;
    if (activityModel.ACTIVITY_STARTDATE && activityModel.ACTIVITY_ENDDATE) {    
        cellView.detail = [NSString stringWithFormat:@"%@ 至 %@", activityModel.ACTIVITY_STARTDATE, activityModel.ACTIVITY_ENDDATE];
    }
    return cell;
}
@end
